/*
 * ItemSearch.java
 *
 * Created on December 13, 2009, 2:39 PM
 */

package itemrecovertool;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Vector;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  Matt
 */
public class ItemSearch extends javax.swing.JPanel {
    
    private MainFrame mainFrame;
    private boolean okBtnPressed = false;
    private Item selectedItem;
    private JDialog diag;
    
    /** Creates new form ItemSearch */
    public ItemSearch(MainFrame mainFrame, JDialog diag) {
        this.mainFrame = mainFrame;
        this.diag = diag;
        initComponents();
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setCellRenderer(new ItemCellRenderer());
        filterBox.setModel(new javax.swing.DefaultComboBoxModel(MainFrame.commonItemTypes));
        
        ListSelectionModel lsm = list.getSelectionModel();
        lsm.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                selectedItem = (Item)list.getSelectedValue();
                okBtn.setEnabled(selectedItem!=null);
            }
        });

    }
    
    public Item getSelectedItem() {
        return okBtnPressed ? selectedItem : null;
    }
    
    public void newSearch() {
        selectedItem = null;
        okBtnPressed = false;
        okBtn.setEnabled(false);
        
        searchField.setText("");
        filterBox.setSelectedIndex(0);
        setBox.setSelected(false);
        updateSearch();
        diag.setVisible(true);
    }
    
    private void updateSearch() {
        String text = searchField.getText();
        String filter = filterBox.getSelectedItem().toString();
        boolean setOnly = setBox.isSelected();
        Vector<Item> l = mainFrame.search(text, filter, setOnly);
        list.setListData(l);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filterBox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        list = new javax.swing.JList();
        cancelBtn = new javax.swing.JButton();
        okBtn = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        setBox = new javax.swing.JCheckBox();
        jButton1 = new javax.swing.JButton();

        filterBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Any Item", "Helmet", "Potion", "Boot", "Scroll" }));
        filterBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterBoxActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(list);

        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelBtnActionPerformed(evt);
            }
        });

        okBtn.setText("Select");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });

        searchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchFieldActionPerformed(evt);
            }
        });

        setBox.setText("Sets");
        setBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setBoxActionPerformed(evt);
            }
        });

        jButton1.setIcon(MainFrame.getIcon("search"));
        jButton1.setContentAreaFilled(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 290, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(okBtn)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cancelBtn))
                    .add(layout.createSequentialGroup()
                        .add(searchField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(filterBox, 0, 241, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(setBox)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(setBox)
                    .add(filterBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelBtn)
                    .add(okBtn))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchFieldActionPerformed
        updateSearch();
}//GEN-LAST:event_searchFieldActionPerformed

    private void filterBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterBoxActionPerformed
        updateSearch();
    }//GEN-LAST:event_filterBoxActionPerformed

    private void setBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setBoxActionPerformed
        updateSearch();
    }//GEN-LAST:event_setBoxActionPerformed

    private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
        diag.setVisible(false);
    }//GEN-LAST:event_cancelBtnActionPerformed

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        okBtnPressed = true;
        diag.setVisible(false);
    }//GEN-LAST:event_okBtnActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        updateSearch();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelBtn;
    private javax.swing.JComboBox filterBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList list;
    private javax.swing.JButton okBtn;
    private javax.swing.JTextField searchField;
    private javax.swing.JCheckBox setBox;
    // End of variables declaration//GEN-END:variables
    
    public static void main(String[] args) throws Exception {
        try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
        catch (Exception e) {}
        
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
            }
        });
    }
    
    
    class ItemCellRenderer extends JLabel implements ListCellRenderer {
        
        private Color normal = Color.black;
        private Color rare = Color.decode("#505091");
        private Color unique = Color.decode("#835408");
        private Color set = Color.decode("#9f12ac");
        
        public ItemCellRenderer() {
            setOpaque(true);
        }

        public Component getListCellRendererComponent(
                       JList list,
                       Object value,
                       int index,
                       boolean isSelected,
                       boolean cellHasFocus) {
            Item i = ((Item)value);
            setText(i.toString());
            setToolTipText(i.NAME+" in "+i.PATH);
            
            Color c = normal;
            if (i.colorID==1||i.colorID==2)
                c = rare;
            else if (i.colorID==3)
                c = unique;
            else if (i.colorID==4)
                c = set;
            setForeground(c);

            if (isSelected) {
                setBackground(list.getSelectionBackground());
            } else {
                setBackground(list.getBackground());
            }
            return this;
        }
    }
}
